<template>
  <div class="listWrap">
    <div class="searchList">
      <el-input placeholder="请输入关键词" v-model="query.keyword"
                @keyup.enter.native="search"></el-input>
      <el-button type="primary" @click="search()">查询</el-button>
    </div>
    <div class="tableContent">
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#F8F8F8'}"
        v-loading = "isLoading"
        element-loading-text="玩命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.5)"
        stripe
        border
        height="800"
      >
      <!--暂无数据提示，自定义-->
        <template slot="empty">
          <img src="../../assets/images/no_data.png"/>
        </template>

        <#list table.fields as field>
            <el-table-column
                    prop="${field.propertyName}"
                    min-width="100"
                    :show-overflow-tooltip="true"
                    label="${(field.comment)!field.propertyName}">
            </el-table-column>
        </#list>

        <el-table-column
          fixed="right"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button @click="update${entity}(scope.row)" type="primary" size="medium">更新</el-button>
            <el-button @click="delete${entity}(scope.row)" type="danger" size="medium">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      background
      :page-size="20"
      :page-sizes="[10, 20, 30, 50, 100]"
      :pager-count="9"
      layout="total, prev, pager, next"
      :total="total"
      @current-change="currentPageChange">
    </el-pagination>
  </div>
</template>
<script>
import API from '@/service/apiService';
export default {
    data () {
        return {
            total: 0,
            tableData: [],
            query: {
                keyword: '',
                currentPage: 1
            },
            isLoading: true
        };
    },
    methods: {
        search () {
            this.query.keyword = this.query.keyword.trim();
            this.getData();
        },
        async getData () {
            this.isLoading = true;
            try {
                const res = await API.${entity?uncap_first}List(this.query);
                if (res.code === 0) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                }
            } catch (e) {
                this.$message.error(e.message);
            }
            this.isLoading = false;
        },
        async update${entity} (row) {
            try {
                const res = await API.${entity?uncap_first}SaveOrUpdate(row);
                if (res.code === 0) {
                    this.$message({
                        type: 'success',
                        message: '更新成功！'
                    });
                    this.getData();
                } else {
                    this.$message({
                        type: 'success',
                        message: '更新失败！'
                    });
                }
            } catch (e) {
                this.$message.error(e.message);
            }
        },
        async delete${entity} (row) {
            try {
                var param = {id: row.id};
                const res = await API.${entity?uncap_first}Delete(param);
                if (res.code === 0) {
                    this.$message({
                        type: 'success',
                        message: '删除成功！'
                    });
                    this.getData();
                } else {
                    this.$message({
                        type: 'success',
                        message: '更新失败！'
                    });
                }
            } catch (e) {
                this.$message.error(e.message);
            }
        },
        currentPageChange (page) {
            this.query.currentPage = page;
            this.getData();
        }
    },
    created () {
        this.getData();
    }
};
</script>
<style lang="scss" scoped>
  .listWrap{
    position: absolute;
    top: 10px;
    bottom: 0;
    right: 0;
    left: 10px;
    background-color: #fff;
    .searchList{
      padding-top: 10px;
      padding-left: 20px;
      .el-input{
        width: 220px;
        height:30px;
      }
      .el-select{
        width: 100px;
        height:30px;
      }
      .el-date-picker{
        width: 120px;
        height:30px;
      }
      .el-icon-search{
        position: absolute;
        top: 13px;
        right: 10px;
        cursor: pointer;
      }
    }
    .tableContent{
      position: absolute;
      border-bottom: none;
      left: 0;
      right: 0;
      top: 60px;
      bottom: 40px;
      overflow: auto;
      .el-table{
        left: 20px;
        right: 20px;
        font-size: 16px;
        position: absolute;
        top: 0;
        bottom: 0;
        width: auto!important;
        height: auto!important;
        overflow: auto;
      }
    }
    .el-pagination{
      position: absolute;
      width: 100%;
      text-align: center;
      bottom: 0px;
    }
  }
</style>
